package org.mock.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.mock.entity.TestExam;
import org.mock.mapper.TestExamMapper;
import org.mock.service.TestExamService;
import org.mock.dto.TestExamDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mock.common.Condition;
import org.mock.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * 服务实现类
 *
 * @author AI
 * @since 2025-08-20
 */
@Service
@AllArgsConstructor
public class TestExamServiceImpl implements TestExamService {

    protected TestExamMapper testExamMapper;

    @Override
    public IPage<TestExam> page(TestExamDTO dto) {
        IPage<TestExam> page = Condition.getPage(dto);
        QueryWrapper<TestExam> queryWrapper = Condition.getQueryWrapper(BeanCopyUtils.copy(dto, TestExam.class));
        if (StrUtil.isNotEmpty(dto.getColumn()) && StrUtil.isNotEmpty(dto.getKeywords())) {
            queryWrapper.like(dto.getColumn(), dto.getKeywords());
        }
        if (StrUtil.isNotEmpty(dto.getOrderAsc())) {
            queryWrapper.orderByAsc(dto.getOrderAsc());
        }
        if (StrUtil.isNotEmpty(dto.getOrderDesc())) {
            queryWrapper.orderByDesc(dto.getOrderDesc());
        }
        if (dto.getStartTime() != null) {
            queryWrapper.gt("created", dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            queryWrapper.lt("created", dto.getEndTime());
        }
        return testExamMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<TestExam> list(TestExamDTO dto) {
        QueryWrapper<TestExam> queryWrapper = Condition.getQueryWrapper(BeanCopyUtils.copy(dto, TestExam.class));
        if (StrUtil.isNotEmpty(dto.getColumn()) && StrUtil.isNotEmpty(dto.getKeywords())) {
            queryWrapper.like(dto.getColumn(), dto.getKeywords());
        }
        if (StrUtil.isNotEmpty(dto.getOrderAsc())) {
            queryWrapper.orderByAsc(dto.getOrderAsc());
        }
        if (StrUtil.isNotEmpty(dto.getOrderDesc())) {
            queryWrapper.orderByDesc(dto.getOrderDesc());
        }
        if (dto.getStartTime() != null) {
            queryWrapper.gt("created", dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            queryWrapper.lt("created", dto.getEndTime());
        }
        return testExamMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(TestExamDTO dto) {
        return testExamMapper.insert(BeanCopyUtils.copy(dto, TestExam.class));
    }

    @Override
    public Integer updateById(TestExamDTO dto) {
        return testExamMapper.updateById(BeanCopyUtils.copy(dto, TestExam.class));
    }


    @Override
    public Integer deleteLogic(List<Integer> toIntList) {
        return testExamMapper.deleteBatchIds(toIntList);
    }

    @Override
    public TestExam getOne(TestExamDTO dto) {
        return testExamMapper.selectOne(Condition.getQueryWrapper(BeanCopyUtils.copy(dto, TestExam.class)));
    }

    @Override
    public Object saveBatch(JSONObject request) {
        JSONArray moduleList = request.getJSONObject("data").getJSONArray("moduleList");
        for (int i = 0; i < moduleList.size(); i++) {
            JSONObject jsonObject = moduleList.getJSONObject(i);
            JSONArray questionList = jsonObject.getJSONArray("questionList");
            for (int j = 0; j < questionList.size(); j++) {
                TestExam testExam = JSONObject.parseObject(questionList.getString(j), TestExam.class);
                JSONArray optionalContent = questionList.getJSONObject(j).getJSONArray("optionalContent");
                StringBuilder sb = new StringBuilder();
                for (int k = 0; k < optionalContent.size(); k++) {
                    JSONObject option = optionalContent.getJSONObject(k);
                    sb.append(option.getString("mark"));
                    sb.append("、");
                    sb.append(option.getString("chooseContent"));
                    sb.append("\n");
                }
                testExam.setOptionalContent(sb.toString());
                testExamMapper.insert(testExam);
            }
        }
        return null;
    }
}
